<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>imehandler_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_events_imehandler_test.js.html">imehandler_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2010 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.events.ImeHandlerTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.events.ImeHandlerTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.array&#39;);
<a name="line19"></a>goog.require(&#39;goog.dom&#39;);
<a name="line20"></a>goog.require(&#39;goog.events&#39;);
<a name="line21"></a>goog.require(&#39;goog.events.ImeHandler&#39;);
<a name="line22"></a>goog.require(&#39;goog.events.KeyCodes&#39;);
<a name="line23"></a>goog.require(&#39;goog.object&#39;);
<a name="line24"></a>goog.require(&#39;goog.string&#39;);
<a name="line25"></a>goog.require(&#39;goog.testing.PropertyReplacer&#39;);
<a name="line26"></a>goog.require(&#39;goog.testing.events&#39;);
<a name="line27"></a>goog.require(&#39;goog.testing.events.Event&#39;);
<a name="line28"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line29"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line30"></a>
<a name="line31"></a>var sandbox;
<a name="line32"></a>var imeHandler;
<a name="line33"></a>var eventsFired;
<a name="line34"></a>var stubs = new goog.testing.PropertyReplacer();
<a name="line35"></a>var eventTypes = goog.events.ImeHandler.EventType;
<a name="line36"></a>
<a name="line37"></a>function setUp() {
<a name="line38"></a>  sandbox = goog.dom.getElement(&#39;sandbox&#39;);
<a name="line39"></a>}
<a name="line40"></a>
<a name="line41"></a>function initImeHandler() {
<a name="line42"></a>  goog.events.ImeHandler.USES_COMPOSITION_EVENTS =
<a name="line43"></a>      goog.userAgent.GECKO ||
<a name="line44"></a>      (goog.userAgent.WEBKIT &amp;&amp; goog.userAgent.isVersionOrHigher(532));
<a name="line45"></a>  imeHandler = new goog.events.ImeHandler(sandbox);
<a name="line46"></a>  eventsFired = [];
<a name="line47"></a>  goog.events.listen(
<a name="line48"></a>      imeHandler,
<a name="line49"></a>      goog.object.getValues(goog.events.ImeHandler.EventType),
<a name="line50"></a>      function(e) {
<a name="line51"></a>        eventsFired.push(e.type);
<a name="line52"></a>      });
<a name="line53"></a>}
<a name="line54"></a>
<a name="line55"></a>function tearDown() {
<a name="line56"></a>  imeHandler.dispose();
<a name="line57"></a>  imeHandler = null;
<a name="line58"></a>
<a name="line59"></a>  stubs.reset();
<a name="line60"></a>}
<a name="line61"></a>
<a name="line62"></a>function tearDownPage() {
<a name="line63"></a>  // Set up a test bed.
<a name="line64"></a>  sandbox.innerHTML = &#39;&lt;div contentEditable=&quot;true&quot;&gt;hello world&lt;/div&gt;&#39;;
<a name="line65"></a>  initImeHandler();
<a name="line66"></a>
<a name="line67"></a>  function unshiftEvent(e) {
<a name="line68"></a>    last10Events.unshift(e.type + &#39;:&#39; + e.keyCode + &#39;:&#39; +
<a name="line69"></a>        goog.string.htmlEscape(goog.dom.getTextContent(sandbox)));
<a name="line70"></a>    last10Events.length = Math.min(last10Events.length, 10);
<a name="line71"></a>    goog.dom.getElement(&#39;logger&#39;).innerHTML = last10Events.join(&#39;&lt;br&gt;&#39;);
<a name="line72"></a>  }
<a name="line73"></a>
<a name="line74"></a>  var last10Events = [];
<a name="line75"></a>  goog.events.listen(
<a name="line76"></a>      imeHandler,
<a name="line77"></a>      goog.object.getValues(goog.events.ImeHandler.EventType),
<a name="line78"></a>      unshiftEvent);
<a name="line79"></a>  goog.events.listen(
<a name="line80"></a>      sandbox,
<a name="line81"></a>      [&#39;keydown&#39;, &#39;textInput&#39;],
<a name="line82"></a>      unshiftEvent);
<a name="line83"></a>}
<a name="line84"></a>
<a name="line85"></a>function assertEventsFired(var_args) {
<a name="line86"></a>  assertArrayEquals(
<a name="line87"></a>      goog.array.clone(arguments), eventsFired);
<a name="line88"></a>}
<a name="line89"></a>
<a name="line90"></a>function fireInputEvent(type) {
<a name="line91"></a>  return goog.testing.events.fireBrowserEvent(
<a name="line92"></a>      new goog.testing.events.Event(type, sandbox));
<a name="line93"></a>}
<a name="line94"></a>
<a name="line95"></a>function fireImeKeySequence() {
<a name="line96"></a>  return fireKeySequence(goog.events.KeyCodes.WIN_IME);
<a name="line97"></a>}
<a name="line98"></a>
<a name="line99"></a>function fireKeySequence(keyCode) {
<a name="line100"></a>  return (
<a name="line101"></a>      goog.testing.events.fireBrowserEvent(
<a name="line102"></a>          new goog.testing.events.Event(&#39;textInput&#39;, sandbox)) &amp;
<a name="line103"></a>      goog.testing.events.fireKeySequence(
<a name="line104"></a>          sandbox, keyCode));
<a name="line105"></a>}
<a name="line106"></a>
<a name="line107"></a>function testHandleKeyDown_GeckoCompositionEvents() {
<a name="line108"></a>  // This test verifies that our IME functions can dispatch IME events to
<a name="line109"></a>  // InputHandler in the expected order on Gecko.
<a name="line110"></a>
<a name="line111"></a>  // Set the userAgent used for this test to Firefox.
<a name="line112"></a>  setUserAgent(&#39;GECKO&#39;);
<a name="line113"></a>  stubs.set(goog.userAgent, &#39;MAC&#39;, false);
<a name="line114"></a>  initImeHandler();
<a name="line115"></a>
<a name="line116"></a>  fireInputEvent(&#39;compositionstart&#39;);
<a name="line117"></a>  assertImeMode();
<a name="line118"></a>
<a name="line119"></a>  fireInputEvent(&#39;compositionupdate&#39;);
<a name="line120"></a>  fireInputEvent(&#39;compositionupdate&#39;);
<a name="line121"></a>
<a name="line122"></a>  fireInputEvent(&#39;compositionend&#39;);
<a name="line123"></a>
<a name="line124"></a>  assertEventsFired(
<a name="line125"></a>      eventTypes.START, eventTypes.UPDATE, eventTypes.UPDATE, eventTypes.END);
<a name="line126"></a>  assertNotImeMode();
<a name="line127"></a>}
<a name="line128"></a>
<a name="line129"></a>
<a name="line130"></a>/**
<a name="line131"></a> * Verifies that our IME functions can dispatch IME events to the input handler
<a name="line132"></a> * in the expected order on Chrome. jsUnitFarm does not have Linux Chrome or
<a name="line133"></a> * Mac Chrome. So, we manually change the platform and run this test three
<a name="line134"></a> * times.
<a name="line135"></a> */
<a name="line136"></a>function testChromeCompositionEventsLinux() {
<a name="line137"></a>  runChromeCompositionEvents(&#39;LINUX&#39;);
<a name="line138"></a>}
<a name="line139"></a>
<a name="line140"></a>function testChromeCompositionEventsMac() {
<a name="line141"></a>  runChromeCompositionEvents(&#39;MAC&#39;);
<a name="line142"></a>}
<a name="line143"></a>
<a name="line144"></a>function testChromeCompositionEventsWindows() {
<a name="line145"></a>  runChromeCompositionEvents(&#39;WINDOWS&#39;);
<a name="line146"></a>}
<a name="line147"></a>
<a name="line148"></a>function runChromeCompositionEvents(platform) {
<a name="line149"></a>  setUserAgent(&#39;WEBKIT&#39;);
<a name="line150"></a>  setVersion(532);
<a name="line151"></a>  stubs.set(goog.userAgent, platform, true);
<a name="line152"></a>  initImeHandler();
<a name="line153"></a>
<a name="line154"></a>  fireImeKeySequence();
<a name="line155"></a>
<a name="line156"></a>  fireInputEvent(&#39;compositionstart&#39;);
<a name="line157"></a>  assertImeMode();
<a name="line158"></a>
<a name="line159"></a>  fireInputEvent(&#39;compositionupdate&#39;);
<a name="line160"></a>  fireInputEvent(&#39;compositionupdate&#39;);
<a name="line161"></a>
<a name="line162"></a>  fireInputEvent(&#39;compositionend&#39;);
<a name="line163"></a>  assertEventsFired(
<a name="line164"></a>      eventTypes.START, eventTypes.UPDATE, eventTypes.UPDATE, eventTypes.END);
<a name="line165"></a>  assertNotImeMode();
<a name="line166"></a>}
<a name="line167"></a>
<a name="line168"></a>
<a name="line169"></a>/**
<a name="line170"></a> * Ensures that the IME mode turn on/off correctly.
<a name="line171"></a> */
<a name="line172"></a>function testHandlerKeyDownForIme_imeOnOff() {
<a name="line173"></a>  setUserAgent(&#39;IE&#39;);
<a name="line174"></a>  initImeHandler();
<a name="line175"></a>
<a name="line176"></a>  // Send a WIN_IME keyDown event and see whether IME mode turns on.
<a name="line177"></a>  fireImeKeySequence();
<a name="line178"></a>  assertImeMode();
<a name="line179"></a>
<a name="line180"></a>  // Send keyDown events which should not turn off IME mode and see whether
<a name="line181"></a>  // IME mode holds on.
<a name="line182"></a>  fireKeySequence(goog.events.KeyCodes.SHIFT);
<a name="line183"></a>  assertImeMode();
<a name="line184"></a>
<a name="line185"></a>  fireKeySequence(goog.events.KeyCodes.CTRL);
<a name="line186"></a>  assertImeMode();
<a name="line187"></a>
<a name="line188"></a>  // Send a keyDown event with keyCode = ENTER and see whether IME mode
<a name="line189"></a>  // turns off.
<a name="line190"></a>  fireKeySequence(goog.events.KeyCodes.ENTER);
<a name="line191"></a>  assertNotImeMode();
<a name="line192"></a>
<a name="line193"></a>  assertEventsFired(
<a name="line194"></a>      eventTypes.START, eventTypes.END);
<a name="line195"></a>}
<a name="line196"></a>
<a name="line197"></a>
<a name="line198"></a>/**
<a name="line199"></a> * Ensures that IME mode turns off when keyup events which are involved
<a name="line200"></a> * in commiting IME text occurred in Safari.
<a name="line201"></a> */
<a name="line202"></a>function testHandleKeyUpForSafari() {
<a name="line203"></a>  setUserAgent(&#39;WEBKIT&#39;);
<a name="line204"></a>  setVersion(531);
<a name="line205"></a>  initImeHandler();
<a name="line206"></a>
<a name="line207"></a>  fireImeKeySequence();
<a name="line208"></a>  assertImeMode();
<a name="line209"></a>
<a name="line210"></a>  fireKeySequence(goog.events.KeyCodes.ENTER);
<a name="line211"></a>  assertNotImeMode();
<a name="line212"></a>}
<a name="line213"></a>
<a name="line214"></a>
<a name="line215"></a>/**
<a name="line216"></a> * SCIM on Linux will fire WIN_IME keycodes for random characters.
<a name="line217"></a> * Fortunately, all Linux-based browsers use composition events.
<a name="line218"></a> * This test just verifies that we ignore the WIN_IME keycodes.
<a name="line219"></a> */
<a name="line220"></a>function testScimFiresWinImeKeycodesGeckoLinux() {
<a name="line221"></a>  setUserAgent(&#39;GECKO&#39;);
<a name="line222"></a>  assertScimInputIgnored();
<a name="line223"></a>}
<a name="line224"></a>
<a name="line225"></a>function testScimFiresWinImeKeycodesChromeLinux() {
<a name="line226"></a>  setUserAgent(&#39;WEBKIT&#39;);
<a name="line227"></a>  setVersion(532);
<a name="line228"></a>  assertScimInputIgnored();
<a name="line229"></a>}
<a name="line230"></a>
<a name="line231"></a>function assertScimInputIgnored() {
<a name="line232"></a>  initImeHandler();
<a name="line233"></a>
<a name="line234"></a>  fireImeKeySequence();
<a name="line235"></a>  assertNotImeMode();
<a name="line236"></a>
<a name="line237"></a>  fireInputEvent(&#39;compositionstart&#39;);
<a name="line238"></a>  assertImeMode();
<a name="line239"></a>
<a name="line240"></a>  fireImeKeySequence();
<a name="line241"></a>  assertImeMode();
<a name="line242"></a>
<a name="line243"></a>  fireInputEvent(&#39;compositionend&#39;);
<a name="line244"></a>  assertNotImeMode();
<a name="line245"></a>}
<a name="line246"></a>
<a name="line247"></a>var userAgents = [&#39;IE&#39;, &#39;GECKO&#39;, &#39;WEBKIT&#39;];
<a name="line248"></a>
<a name="line249"></a>function setUserAgent(userAgent) {
<a name="line250"></a>  for (var i = 0; i &lt; userAgents.length; i++) {
<a name="line251"></a>    stubs.set(goog.userAgent, userAgents[i], userAgents[i] == userAgent);
<a name="line252"></a>  }
<a name="line253"></a>}
<a name="line254"></a>
<a name="line255"></a>function setVersion(version) {
<a name="line256"></a>  goog.userAgent.VERSION = version;
<a name="line257"></a>  goog.userAgent.isVersionOrHigherCache_ = {};
<a name="line258"></a>}
<a name="line259"></a>
<a name="line260"></a>function assertImeMode() {
<a name="line261"></a>  assertTrue(&#39;Should be in IME mode.&#39;, imeHandler.isImeMode());
<a name="line262"></a>}
<a name="line263"></a>
<a name="line264"></a>function assertNotImeMode() {
<a name="line265"></a>  assertFalse(&#39;Should not be in IME mode.&#39;, imeHandler.isImeMode());
<a name="line266"></a>}
</pre>


</body>
</html>
